<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>抽奖活动</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
	<style>
	#urlqr img{margin:0 auto}
	</style>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header">
						<i class="fa fa-list"></i> 抽奖活动列表 {if input('param.isopen')==1}<i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i>{/if}
						<!-- <i class="layui-icon layui-icon-close" style="font-size:18px;font-weight:bold;cursor:pointer" onclick="closeself()"></i> -->
					</div>
          <div class="layui-card-body" pad15>
						<div class="layui-col-md6" style="padding-bottom:10px">
							<a class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="openmax('{:url('edit')}/type/dzp')"><i class="fa fa-plus"></i> 大转盘</a>
							<a class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="openmax('{:url('edit')}/type/ggk')"><i class="fa fa-plus"></i> 刮刮卡</a>
							<!-- <a class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="openmax('{:url('edit')}/type/zjd')"><i class="fa fa-plus"></i> 砸金蛋</a>
							<a class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="openmax('{:url('edit')}/type/yyl')"><i class="fa fa-plus"></i> 摇摇乐</a> -->
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="datadel(0)">删除</button>
						</div>
						<div class="layui-form layui-col-md6 layui-form-search">
							<div class="layui-inline layuiadmin-input-useradmin">
								<label class="layui-form-label">活动名称</label>
								<div class="layui-input-inline">
									<input type="text" name="name" autocomplete="off" class="layui-input" value="">
								</div>
							</div>
							<div class="layui-inline">
								<button class="layui-btn layuiadmin-btn-replys" lay-submit="" lay-filter="LAY-app-forumreply-search">
									<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
								</button>
							</div>
						</div>
						<div class="layui-col-md12">
							<table id="tabledata" lay-filter="tabledata"></table>
						</div>
          </div>
        </div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
	layui.laydate.render({ 
		elem: '#ctime',
		trigger: 'click',
		range: '~' //或 range: '~' 来自定义分割字符
	});
  var table = layui.table;
	var datawhere = {};
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: "{$Request.url}" //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
			{type:"checkbox"},
      {field: 'id', title: 'ID',width:80},
      {field: 'type', title: '类型',templet:function(d){
				if(d.type == 'dzp') return '大转盘';
				if(d.type == 'ggk') return '刮刮卡';
				if(d.type == 'zjd') return '砸金蛋';
				if(d.type == 'yyl') return '摇摇乐';
				return '';
      },width:100},
      {field: 'name', title: '活动名称'},
      {field: 'jpnum', title: '总奖品数',width:100,templet:function(d){ return d.j1sl + d.j2sl + d.j3sl + d.j4sl + d.j5sl + d.j6sl + d.j7sl + d.j8sl + d.j9sl + d.j10sl + d.j11sl + d.j12sl}},
      {field: 'cjnum', title: '已抽奖次数',width:100},
      {field: 'zjnum', title: '已中奖次数',width:100},
      {field: 'starttime', title: '活动时间',templet:function(d){ return date('Y-m-d H:i:s',d.starttime) + ' ~ <br>'+date('Y-m-d H:i:s',d.endtime)},width:180},
      {field: 'status', title: '状态',templet:function(d){ 
				if(d.status==0){
					return '<span style="color:red">未开启</span>';
				}else{
					if(d.starttime > {:time()}){
						return '<button class="layui-btn layui-btn-sm" style="background-color:orange">未开始</button>';
					}else if(d.endtime < {:time()}){
						return '<button class="layui-btn layui-btn-sm" style="background-color:#888">已结束</button>';
					}else if(d.endtime > {:time()} && d.starttime < {:time()}){
						return '<button class="layui-btn layui-btn-sm" style="background-color:#3e5">进行中</button>';
					}
				}
			}},
      {field: 'operation', title: '操作',width:200,templet:function(d){
				var html = '';
				html += '<button class="table-btn" onclick="openmax(\'{:url('edit')}/id/'+d.id+'\')">编辑</button>';
				html += '<button class="table-btn" onclick="datadel('+d.id+')">删除</button>';
				html += '<button class="table-btn" onclick="showurl('+d.id+',\''+d.type+'\')">查看链接</button>';				
				html += '<button class="table-btn" onclick="openmax(\'{:url('record')}/isopen/1/hid/'+d.id+'\')">抽奖记录</button>';
				{if getcustom('choujiang_qrcode') && $auth_data=='all' || in_array('ChoujiangQrcode/*',$auth_data)}
				html += '<button class="table-btn" onclick="openmax(\'{:url('ChoujiangQrcode/qrcode')}/isopen/1/hid/'+d.id+'\')">抽奖码列表</button>';
				if(d.endtime > {:time()}){
					html += '<button class="table-btn" onclick="makecode('+d.id+')">生成抽奖码</button>';
				}
				
				html += '<button class="table-btn" onclick="download(\''+d.id+'\')">下载抽奖码</button>';
				{/if}
				return html;
			}}
    ]]
  });
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	});
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			var ids = [];
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id)
		}
		layer.confirm('确定要删除吗？删除后无法恢复！',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}
	function showurl(id,type){
        {if $bid > 0}
        if(type == 'dzp') var pagepath = 'activity/xydzp/index?bid={$bid}&id='+id;
        if(type == 'ggk') var pagepath = 'activity/ggk/index?bid={$bid}&id='+id;
        {else}
        if(type == 'dzp') var pagepath = 'activity/xydzp/index?id='+id;
        if(type == 'ggk') var pagepath = 'activity/ggk/index?id='+id;
        {/if}
		viewLink(pagepath);
	}
	//生成抽奖码
	var choujiang_qrcode_miniqrcode = false;
	{if getcustom('choujiang_qrcode_miniqrcode')}
		choujiang_qrcode_miniqrcode = true;
	{/if}
	{if getcustom('choujiang_qrcode')}
	var pagenum = 0;
	var pagelimit = 200;//每次生成多少个
	var total_pagelimit = 0;
	function makecode(hid,cardno_start){
		var html = '<div style="margin:20px auto;">';
		html+='<div class="layui-form" lay-filter="">';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">生成数量：</label>';
		html+='		<div class="layui-input-inline" style="width:300px">';
		html+='			<input type="text" name="makecount" value="100" class="layui-input">';
		html+='		</div>';
		html+='		<div class="layui-form-mid layui-word-aux" style="margin-left:10px;"></div>';
		html+='	</div>';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">兑换码位数：</label>';
		html+='		<div class="layui-input-inline" style="width:300px">';
		html+='			<input type="text" name="codelength" value="8" class="layui-input">';
		html+='		</div>';
		html+='		<div class="layui-form-mid layui-word-aux" style="margin-left:10px;">位数控制在6-16位</div>';
		html+='	</div>';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">兑换码类型：</label>';
		html+='		<div class="layui-input-inline" style="width:300px">';
		html+='			<input type="radio" name="codetype" value="1" title="纯数字" checked>';
		html+='			<input type="radio" name="codetype" value="2" title="大小写字母">';
		html+='			<input type="radio" name="codetype" value="3" title="小写字母">';
		html+='			<input type="radio" name="codetype" value="4" title="大写字母">';
		html+='			<input type="radio" name="codetype" value="5" title="数字+小写字母">';
		html+='			<input type="radio" name="codetype" value="6" title="数字+大写字母">';
		html+='		</div>';
		html+='	</div>';
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">图形码类型：</label>';
		html+='		<div class="layui-input-inline" style="width:380px">';
		html+='			<input type="radio" name="qrtype" value="1" title="链接二维码" checked>';
		html+='			<input type="radio" name="qrtype" value="2" title="小程序码">';
		if(choujiang_qrcode_miniqrcode){
			html+='		<input type="radio" name="qrtype" value="3" title="小程序二维码">';
		}
		html+='		</div>';
		html+='	</div>';
		{if $choujiang_qrcode_remark == 1}
		html+='	<div class="layui-form-item">';
		html+='		<label class="layui-form-label" style="width:150px">备注：</label>';
		html+='		<div class="layui-input-inline" style="width:200px">';
		html+='			<input type="text" name="remark" value="" class="layui-input">';
		html+='		</div>';
		html+='	</div>';
		{/if}
		html+='	<div class="layui-form-item" style="margin-top:30px">';
		html+='		<label class="layui-form-label" style="width:150px"></label>';
		html+='		<div class="layui-input-inline">';
		html+='			<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit_excel" id="scnn">生 成</button>';
		html+='		</div>';
		html+='	</div>';
		html+='</div>';
		html+='</div>'
		var makelayer = layer.open({type:1,area: ['800px', '460px'],title:'生成抽奖码',content:html,shadeClose:true})
		layui.form.render();
		layui.form.on('submit(submit_excel)', function(obj){
			$('#scnn').text('生成中...')
			$('#scnn').css('background-color','#CCC')
			var field = obj.field;
			field.hid = hid;
			pagenum = 0;
			total_pagelimit = 0;
			var mcscnum = pagelimit;
			if(field.makecount < pagelimit){
				mcscnum = field.makecount;
			}
			_domakecode(field,mcscnum,makelayer);
		})
	}
	function _domakecode(field,mcscnum,makelayer){
		var index= layer.load();
		pagenum++;
		field.mcscnum = mcscnum;
		$.post("{:url('ChoujiangQrcode/makecode')}",field,function(data){
			if(data.status==1){
				total_pagelimit += data.sucnum;
				layer.msg(data.msg,{offset:'100px'});
			}
			if(total_pagelimit < field.makecount && data.status==1){
				var mcscnum = pagelimit;
				var cz = field.makecount - total_pagelimit;
				if(cz<pagelimit){
					mcscnum = cz;
				}
				_domakecode(field,mcscnum,makelayer)
			}else {
				layer.close(index);
				if(data.status ==0){
					$('#scnn').text('生 成')
					$('#scnn').css('background-color','#1E9FFF')
					dialog(data.msg,data.status);
				}else {
					layer.close(makelayer);
					dialog('成功生成'+total_pagelimit+'个抽奖码',data.status);
				}
				tableIns.reload()
			}
		})
	}
	function download(id){
		layer.confirm('确定要下载吗?',{icon: 7, title:'操作确认'}, function(index){
		layer.close(index);
		var index = layer.load();
		$.post("{:url('ChoujiangQrcode/listdownload')}/hid/"+id+urlEncode(datawhere),{},function(data){
			layer.close(index);
			dialog(data.msg,data.status,data.url)
		})
		});
	}
	{/if}
	</script>
	{include file="public/copyright"/}
	<div id="NewsToolBox"></div>
</body>
</html>